package controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import entity.Teacher;
import exception.BaseException;
import service.TeacherService;
import util.Condition;
import util.ControllerHelper;
import util.JSONUtil;
import util.Pagination;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/teacherReadCtl")
public class TeacherReadCtl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws
            ServletException, IOException {
        JSONObject message=new JSONObject();
        resp.setContentType("application/json;charset=utf-8");
        String req_jsonStr= JSONUtil.getJSON(req);

        JSONObject req_jsonObj= JSON.parseObject(req_jsonStr);

        if(req_jsonObj==null){
            message.put("message","未收到信息");
            resp.getWriter().println(message);
            return;
        }

        try{
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            Collection<Teacher> teacherList= TeacherService.getInstance().findAll(conditionList);
            for(Teacher teacher:teacherList){
                System.out.println(teacher);
            }
            String teacher_jsonStr=JSON.toJSONString(teacherList,JSONWriter.Feature.ReferenceDetection);
            System.out.println(teacher_jsonStr);
            resp.getWriter().print(teacher_jsonStr);
        }catch (BaseException e){
            message.put("message",e.getMessage());
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (SQLException e){
            message.put("message","数据库异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (Exception e){
            message.put("message","网络异常");
            resp.getWriter().println(message);
        }

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        JSONObject message=new JSONObject();
        resp.setContentType("application/json;charset=utf-8");
        String req_jsonStr= JSONUtil.getJSON(req);
        JSONObject req_jsonObj= JSON.parseObject(req_jsonStr);
        if(req_jsonObj==null){
            message.put("message","未收到信息");
            resp.getWriter().println(message);
            return;
        }
        try{
            Pagination pagination=ControllerHelper.getPagination(req_jsonObj);
            List<Condition> conditionList=ControllerHelper.getConditions(req_jsonObj);
            Collection<Teacher> teachers=TeacherService.getInstance().findAll(conditionList,pagination);
            message.put("totalNum",pagination.getTotalNum());
            message.put("records",teachers);
            resp.getWriter().println(message);
        }catch (BaseException e){
            message.put("message",e.getMessage());
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (SQLException e){
            message.put("message","数据库异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (Exception e){
            message.put("message","网络异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }
    }
    /*
    {
    "conditionList":[
        {
            "value":"T","key":"name","operator":"LIKE"
        }
        ,
        {
            "value":"1","key":"department_id","operator":"="
        }
    ]
}
     */
}
